package com.imooc.base;

import com.imooc.entity.User;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @ClassName LoginUserUtil
 * @Description TODO
 * @Author Wxg
 * @Date 2018/11/4 13:27
 * @Version 1.0
 */
public class LoginUserUtil {

    public static User load() {
        Object principal = SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();

        if (principal != null && principal instanceof User) {
            return (User) principal;
        }

        return null;
    }

    public static Long getLoginUserId() {
        User user = load();
        if (user == null) {
            return -1L;
        }
        return user.getId();
    }
}
